package org.yanfengying.org.controller;

import org.springframework.http.HttpRequest;
import org.yanfengying.constants.BaseConstants;
import org.yanfengying.dto.ShopResouceDTO;
import org.yanfengying.org.service.IShopService;
import org.yanfengying.org.domain.Shop;
import org.yanfengying.org.query.ShopQuery;
import org.yanfengying.utils.AjaxResult;
import org.yanfengying.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yanfengying.utils.RedisUtils;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.insert(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Shop shop = shopService.loadById(id);
            return AjaxResult.me().setResult(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< Shop> list = shopService.loadAll();
            return AjaxResult.me().setResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody ShopQuery query)
    {
        try {
            PageList pageList = shopService.pageList(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop)
    {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("入驻失败！"+e.getMessage());
        }
    }

    /**
     * 自动审核通过以后->发送邮件激活
     * @param iduuid
     * @return
     */
    @GetMapping("/sshop/{iduuid}")
    public AjaxResult saveShop (@PathVariable("iduuid") String iduuid)
    {
        try {
            int length = iduuid.length();
            String id1 = iduuid.substring(0, length-4);
            Long id = Long.valueOf(id1);
            String uuid = iduuid.substring(length - 4, length);
            Shop shop = shopService.loadById(id);
            //判断uuid是否过期   uuid.equals(RedisUtils.INSTANCE.get("uu"))
            if(uuid.equals(RedisUtils.INSTANCE.get("uu"))){//等于就没过期
                //店铺状态为已激活
                shop.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
                //店长状态为已激活
                return AjaxResult.me();
            }
                //店铺状态为已激活
                shop.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
                shopService.insert(shop);
                //店长状态为已激活
                return AjaxResult.me().setSuccess(false).setMessage("激活码已过期");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("激活失败！"+e.getMessage());
        }
    }
    /**
     * 人工审核->通过
     * @param dto
     * @return
     */
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody ShopResouceDTO dto)
    {
        try {
            shopService.pass(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核失败！"+e.getMessage());
        }
    }
    /**
     * 人工审核->驳回
     * @param dto
     * @return
     */
    @PostMapping("/nopass")
    public AjaxResult nopass(@RequestBody ShopResouceDTO dto)
    {
        try {
            shopService.nopass(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核失败！"+e.getMessage());
        }
    }
}
